package com.jbm.jbmsupplier.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.activitymanag.Activity_Manage;
import com.jbm.jbmsupplier.activity.loadhomepage.view.ListFrement_Construction;
import com.jbm.jbmsupplier.activity.repairs.FixCategoryAty;
import com.jbm.jbmsupplier.activity.searchconstrucation.view.SearContruction;
import com.jbm.jbmsupplier.activity.searchmanage.view.SearchManage;
import com.jbm.jbmsupplier.adapter.FragmentAdapter;
import com.jbm.jbmsupplier.frement.ListFrement;
import com.jbm.jbmsupplier.presenters.NewHomeActivitys;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.utlis.PtesonalPrivile;
import com.jbm.jbmsupplier.views.NewHomeView;
import com.jbm.jbmsupplier.zp.liaotian.Recent_ContactsActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static android.R.attr.tag;


/**
 * Created by 家帮忙 on 2017/4/10.
 */

public class NewHomeActivity extends AllActivityApp implements NewHomeView {
    @InjectView(R.id.tabs)
    TabLayout tabs;
    @InjectView(R.id.viewpagerfb)
    ViewPager viewpagerfb;
    List<Fragment> fragments;
    final String Order_Type_new = "new";
    final String Order_Type_break = "break";
    final String Order_Type_confirm = "confirm";
    final String Order_Type_submit = "submit";
    final String Order_Type_payed="payed";
    @InjectView(R.id.toolbar_image)
    ImageView toolbarImage;
    @InjectView(R.id.linnerlayout)
    LinearLayout linnerlayout;
    @InjectView(R.id.tetoobartitle)
    TextView tetoobartitle;


    private NewHomeActivitys newHomeActivitys;

    private Toolbar toolbars;
    private ActionBarDrawerToggle mDrawerToggle;
    private TextView tename;
    private NavigationView navigationView;
    private DrawerLayout mDrawerLayout;
    private String construcation;
    private String pri;
    private TextView tenameuser;

    @Override
    public int setContentView() {
        return R.layout.newhomeac_lz;
    }
    @Override
    public void initIalizeView() {
        ExitActivity.getInstance().addActivity(this);
        ButterKnife.inject(this);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.draw_left);
        toolbars = (Toolbar) findViewById(R.id.to_custro);
        navigationView = (NavigationView) findViewById(R.id.navigation);
        newHomeActivitys = new NewHomeActivitys(this, this);
        newHomeActivitys.setInitMode();
        SetOnClicklin();
    }
    private void SetOnClicklin() {
//        toolbars.setTitleTextColor(Color.WHITE);
        toolbars.setTitle("");
        setSupportActionBar(toolbars);
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbars, R.string.open, R.string.cloce);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        //设置菜单列表
        navigationView.setItemIconTintList(null);//设置为空图片颜色正常  不设置为灰色
        //获取头布局文件  
        View headerVie = navigationView.getHeaderView(0);
        tename = (TextView) headerVie.findViewById(R.id.teheadname);//登录人的手机号
        tenameuser = (TextView) headerVie.findViewById(R.id.teheadusername);//登录人的名字
        tenameuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newHomeActivitys.setDialogUserName();
            }
        });
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.fnbwx://内部维修
                        mDrawerLayout.closeDrawers();
                        if (pri!=null){
                        if (pri.contains("保修")) {
                            startActivity(new Intent(NewHomeActivity.this, FixCategoryAty.class));
                        } else {
                            Toast.makeText(NewHomeActivity.this, "您没有对应的权限", Toast.LENGTH_SHORT).show();
                        }}
                        break;
                    case R.id.fsy://首页
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.fqhjs://权限管理
                        mDrawerLayout.closeDrawers();
                        if (pri!=null){
                        if (pri.contains("经理") || pri.contains("副经理")) {
                            startActivity(new Intent(NewHomeActivity.this, SearchManage.class));
                        } else {
                            Toast.makeText(NewHomeActivity.this, "您没有对应的权限", Toast.LENGTH_SHORT).show();
                        }}
                        break;
                    case R.id.fqb://钱包
                        startActivity(new Intent(NewHomeActivity.this, PledgeList.class));
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.fxgmm://修改密码
                        startActivity(new Intent(NewHomeActivity.this, AmendPwass.class));
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.sggl://服务端
                        mDrawerLayout.closeDrawers();
                        if (pri!=null){
                        if (pri.contains("经理") && pri.contains("副经理")&&pri.contains("作业")) {
                            startActivity(new Intent(NewHomeActivity.this, Activity_Manage.class).putExtra("home", "manage"));
                        } else {
                            Toast.makeText(NewHomeActivity.this, "您没有对应的权限", Toast.LENGTH_SHORT).show();
                        }}
                        break;
                    case R.id.exitlogin://退出登录
                        newHomeActivitys.SetExitLogin();
                        mDrawerLayout.closeDrawers();
                        break;
                }
                return true;
            }

        });
        tetoobartitle.setText("家帮忙服务端");
    }

    @Override
    public void getLoadTitle() {//加载标题
        pri = new PtesonalPrivile().getPrson(getApplication());
        if (pri.contains("经理") || pri.contains("副经理") ) {
            Picasso.with(this).load(R.mipmap.sousuo_home).into(toolbarImage);
            setTitle("待分配", "归档订单", "家帮忙","", 2);//代表管理端
            return;
        } else if (pri.contains("作业")) {//代表服务端
            toolbarImage.setVisibility(View.GONE);
//            Picasso.with(this).load(R.mipmap.xiaoxi).into(toolbarImage);
            setTitle("待确认", "作业中", "未结算","已结算", 1);
            return;
        } else if (pri.equals("")) {
            linnerlayout.setVisibility(View.GONE);
            toolbarImage.setVisibility(View.GONE);
            tetoobartitle.setText("您没有任何操作权限");
            return;
        }

    }

    @Override
    public void getExit(String name, Handler handler, int obj, int tag) {
        if (tag == 1) {
            hint(name, handler, obj);
        }
    }

    @Override
    public TextView getTextUser() {
        return tename;
    }

    @Override
    public TextView getTextUserName() {
        return tenameuser;
    }

    @OnClick(R.id.toolbar_image)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_image:
                if (pri!=null){
                if (pri.contains("经理") || pri.contains("副经理")) {
                    //代表管理端
                    startActivity(new Intent(this, SearContruction.class));
                    return;
                }
                else if (pri.contains("作业")) {//代表服务端
                    startActivity(new Intent(this, Recent_ContactsActivity.class));
                    return;
                }
                }
                break;
        }
    }



    public interface getDateType {//施工段接口

        void setType(int i, String sta);
    }

    public interface DateTypeCon {//管理端接口

        void setDateTypeCon(int i, String sta);
    }


    public void setTitle(String name, String name1, String name2,String name3, final int tag) {//设置标题栏
        List<String> titles = new ArrayList<>();
        titles.add(name);
        titles.add(name1);
        titles.add(name2);
        if (!name3.equals("")){
        titles.add(name3);}
        for (int i = 0; i < titles.size(); i++) {
            tabs.addTab(tabs.newTab().setText(titles.get(i)));
        }
        fragments = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            if (pri.contains("经理") || pri.contains("副经理") ) {
                //代表管理端
                fragments.add(new ListFrement_Construction());
            } else if (pri.contains("作业")) {//代表服务端
                fragments.add(new ListFrement());

            }

        }

        FragmentAdapter mFragmentAdapteradapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置适配器
        viewpagerfb.setAdapter(mFragmentAdapteradapter);
        //将TabLayout和ViewPager关联起来。
        tabs.setupWithViewPager(viewpagerfb);
        //给TabLayout设置适配器
        tabs.setTabsFromPagerAdapter(mFragmentAdapteradapter);

        viewpagerfb.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String mQueryStatus = null;
             if (pri.contains("经理") || pri.contains("副经理")) {
                    //代表管理端
                    switch (position) {
                        case 0://待分配
                            break;
                        case 1://订单归档
                            break;
                        case 2://家帮忙
                            break;
                    }
                    ((ListFrement_Construction) fragments.get(position)).setDateTypeCon(position, mQueryStatus);
                 return;
                }else    if (pri.contains("作业")) {//代表服务端
                 switch (position) {
                     case 0:
                         mQueryStatus = "'" + Order_Type_new + "','" + Order_Type_break + "'";
                         break;
                     case 1:
                         mQueryStatus = "'" + Order_Type_confirm + "'";
                         break;
                     case 2:
                         mQueryStatus = "'" + Order_Type_submit + "'";
                         break;
                     case 3:
                         mQueryStatus = "'" + Order_Type_payed + "'";
                         break;
                 }
                 ((ListFrement) fragments.get(position)).setType(position, mQueryStatus);
             }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    long che = 0;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - che > 2000) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            che = System.currentTimeMillis();
        } else {
            ExitActivity.getInstance().exitActivity();
            newHomeActivitys.SetonBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        newHomeActivitys.setonDestroy();
    }


}
